package com.turing.post.cardmanagement.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.cardmanagement.util.CardManaApplicationException;
import com.turing.post.cardmanagement.util.CardManufacture;
import com.turing.post.cardmanagement.util.CardManufactureBusiness;
import com.turing.post.cardmanagement.util.CardManufactureDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * 制卡商管理业务层
 * 
 * @author 李治
 * 
 */

@Stateless(mappedName = "cardmanagement.impl.CardManufactureBusinessImpl")
@Remote
@Local
public class CardManufactureBusinessImpl implements CardManufactureBusiness {

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;
	@EJB(mappedName = "cardmanagement.impl.CardManufactureDaoImpl")
	private CardManufactureDao cardManufactureDaoImpl;

	@Override
	public void deleteCardManufactureByCardManufactureId(PCPSWebInput webInput,
			BigInteger cardManufactureId) throws PCPSApplicationException {
		if (null == cardManufactureId || "".equals(cardManufactureId))
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"参数为空!");

		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Delete, "根据制卡商ID删除制卡商",
				"cardManufactureId:" + cardManufactureId);
		cardManufactureDaoImpl
				.deleteCardManufactureByCardManufactureId(cardManufactureId);
	}

	@Override
	public CardManufacture getCardManufactureByCardManufactureId(
			BigInteger cardManufactureId) {
		return cardManufactureDaoImpl
				.getCardManufactureByCardManufactureId(cardManufactureId);
	}

	@Override
	public List<CardManufacture> getCardManufactures(
			String cardManufactureName, String issueCompanyNo, String status,
			String linkMan, Integer page) {
		return cardManufactureDaoImpl.getCardManufactures(cardManufactureName,
				issueCompanyNo, status, linkMan, page);
	}

	@Override
	public BigInteger getCardManufacturesCount(String cardManufactureName,
			String issueCompanyNo, String status, String linkMan) {
		return cardManufactureDaoImpl.getCardManufacturesCount(
				cardManufactureName, issueCompanyNo, status, linkMan);
	}

	@Override
	public void insertCardManufacture(PCPSWebInput webInput,
			CardManufacture cardManufacture) throws PCPSApplicationException {
		if (null == cardManufacture)
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"参数为空!");
		if (PCPSUtil.isEmpty(cardManufacture.getIssueCompanyNo()))
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"发卡商为空!");
		if (PCPSUtil.isEmpty(cardManufacture.getCardManufactureName()))
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"制卡商名称为空!");
		if (PCPSUtil.isEmpty(cardManufacture.getStatus()))
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"状态为空!");
		cardManufactureDaoImpl.insertCardManufacture(cardManufacture);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "新增制卡商", "CardManufactureName:"
						+ cardManufacture.getCardManufactureName());
	}

	@Override
	public void updateCardManufacture(PCPSWebInput webInput,
			CardManufacture cardManufacture) throws PCPSApplicationException {
		if (null == cardManufacture
				|| null == cardManufacture.getCardManufactureId())
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"参数为空!");
		CardManufacture cmf = cardManufactureDaoImpl
				.getCardManufactureByCardManufactureId(cardManufacture
						.getCardManufactureId());
		if (null == cmf)
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"无此制卡商!");
		if (PCPSUtil.isEmpty(cardManufacture.getCardManufactureName()))
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"制卡商名称为空!");
		if (PCPSUtil.isEmpty(cardManufacture.getIssueCompanyNo()))
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"发卡商为空!");
		if (PCPSUtil.isEmpty(cardManufacture.getStatus()))
			throw new CardManaApplicationException(
					PCPSApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
					"状态为空!");
		if (!PCPSUtil.isEmpty(cardManufacture.getCardManufactureName()))
			cmf
					.setCardManufactureName(cardManufacture
							.getCardManufactureName());
		if (!PCPSUtil.isEmpty(cardManufacture.getLinkMan()))
			cmf.setLinkMan(cardManufacture.getLinkMan());
		if (!PCPSUtil.isEmpty(cardManufacture.getStatus()))
			cmf.setStatus(cardManufacture.getStatus());
		if (!PCPSUtil.isEmpty(cardManufacture.getLinkTele()))
			cmf.setLinkTele(cardManufacture.getLinkTele());
		if (!PCPSUtil.isEmpty(cardManufacture.getAddress()))
			cmf.setAddress(cardManufacture.getAddress());
		if (!PCPSUtil.isEmpty(cardManufacture.getRemark()))
			cmf.setRemark(cardManufacture.getRemark());
		cardManufactureDaoImpl.updateCardManufacture(cmf);
		// 日志记录
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "新增制卡商", "CardManufactureName:"
						+ cmf.getCardManufactureName());
	}
}